home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol07 / 07 / colrdrop / coolcolr.c < prev    next >
C/C++ Source or Header  |  1992-11-01  |  12KB  |  378 lines

  1. //****************************************************************************
  2. //      File:  COOLCOLR.C                                                  
  3. //                                                                         
  4. //   Purpose:  
  5. //                                                                         
  6. // Functions:  
  7. //             
  8. //                                                                         
  9. // Development Team:
  10. //
  11. //       Greg Keyser
  12. //
  13. // Written by Microsoft Product Support Services, Windows Developer Support
  14. // Copyright (c) 1992 Microsoft Corporation. All rights reserved.
  15. //****************************************************************************
  16.  
  17. #include <windows.h>
  18. #include <cpl.h>
  19. #include "commdlg.h"
  20. #include "global.h"
  21.  
  22. LONG FAR PASCAL CPlApplet(HWND, WORD, LONG, LONG);
  23. BOOL FAR PASCAL LibMain(HANDLE, WORD, WORD, LPSTR);
  24.  
  25. // we put LibMain and CPlApplet functions in the _INIT segment to make
  26. // the LibMain and CPlApplet discardable.
  27.  
  28. #pragma alloc_text( _INIT, LibMain, CPlApplet )
  29.  
  30. //****************************************************************************
  31. // Function: SaveToWinIni
  32. //
  33. // Purpose:  To save the new color settings to WIN.INI.  Rather than try to
  34. //           keep track of what's changed and what hasn't, I just get all
  35. //           the current system colors and write them out to WIN.INI.
  36. //           Simplicity prevails in this case.
  37. //
  38. // Parameters: None
  39. //
  40. // Returns : Nothing
  41. //
  42. // Comments:
  43. //
  44. // History:  Date       Author        Reason
  45. //           3/24/92    gak           Created
  46. //****************************************************************************
  47. void FAR PASCAL SaveToWinIni(void)
  48. {
  49.    char  *pszWinStrings[] = {
  50.                          "MenuText",
  51.                          "Menu",
  52.                          "TitleText",
  53.                          "ActiveTitle",
  54.                          "InactiveTitleText",
  55.                          "InactiveTitle",
  56.                          "WindowFrame",
  57.                          "InactiveBorder",
  58.                          "Scrollbar",
  59.                          "WindowText",
  60.                          "Window",
  61.                          "Background",
  62.                          "AppWorkspace",
  63.                          "ActiveBorder",
  64.                          "GrayText",
  65.                          "Hilight",
  66.                          "HilightText",
  67.                          "ButtonHilight",
  68.                          "ButtonShadow",
  69.                          "ButtonText",
  70.                          "ButtonFace"
  71.                         };
  72.  
  73.    char szColors[]="colors";
  74.    char szBuffer[128];
  75.  
  76.    BYTE Red, Green, Blue;
  77.    WORD wCtr;
  78.  
  79.    GetCurrentSysColors();
  80.  
  81.    for (wCtr=IDC_COLOR_MENUTEXT; wCtr<=IDC_COLOR_BTNFACE; wCtr++)
  82.       {
  83.          Red  =GetRValue(gdwOldSysColors[wCtr]);
  84.          Green=GetGValue(gdwOldSysColors[wCtr]);
  85.          Blue =GetBValue(gdwOldSysColors[wCtr]);
  86.          wsprintf(szBuffer, "%u %u %u", (WORD)(unsigned int)Red,
  87.                   (WORD)(unsigned int)Green, (WORD)(unsigned int)Blue);
  88.          WriteProfileString(szColors, pszWinStrings[wCtr], szBuffer);
  89.       }
  90.    
  91.    return;
  92. }
  93.  
  94. //****************************************************************************
  95. // Function: GetCurrentSysColors
  96. //
  97. // Purpose:  To initialize a global array that will be used if the user  
  98. //           CANCEL's his changes.
  99. //
  100. // Parameters: None
  101. //
  102. // Returns : Nothing
  103. //
  104. // Comments:
  105. //
  106. // History:  Date       Author        Reason
  107. //           3/23/92    gak           Created
  108. //****************************************************************************
  109. void FAR PASCAL GetCurrentSysColors(void)
  110. {
  111.    WORD wCtr;
  112.  
  113.    for (wCtr=IDC_COLOR_MENUTEXT; wCtr<=IDC_COLOR_BTNFACE; wCtr++)
  114.       gdwOldSysColors[wCtr]=GetSysColor(giSysColorArea[wCtr]);
  115.      
  116.    return;
  117. }
  118.  
  119. //****************************************************************************
  120. // Function: InitializeStruct
  121. //
  122. // Purpose:  To initialize a structure for the current common dialog.    
  123. //           This routine is called just before the common dialogs       
  124. //           API is called.                                              
  125. //
  126. // Parameters:
  127. //    wCommDlgType == Indicates the type of common dialog we need to initialize
  128. //    lpStruct     == Long pointer to memory block for the structure
  129. //
  130. // Returns : Nothing
  131. //
  132. // Comments:
  133. //
  134. // History:  Date       Author        Reason
  135. //           2/19/92    gregk         Created
  136. //****************************************************************************
  137. void FAR PASCAL InitializeStruct(WORD wCommDlgType, LPSTR lpStruct)
  138. {
  139.  
  140.    LPCOLORSCHUNK       lpColorsChunk;
  141.    WORD                wCtr;
  142.    HDC                 hDC;
  143.    
  144.    switch (wCommDlgType)
  145.       {
  146.       case IDC_COLORS:
  147.  
  148.          lpColorsChunk = (LPCOLORSCHUNK)lpStruct;
  149.  
  150.          hDC = GetDC(ghWnd);
  151.          lpColorsChunk->dwCustClrs[0]=GetBkColor(hDC);
  152.          ReleaseDC(ghWnd, hDC);
  153.  
  154.          for (wCtr=1; wCtr<=15; wCtr++)
  155.             lpColorsChunk->dwCustClrs[wCtr]= lpColorsChunk->dwCustClrs[0];
  156.  
  157.          lpColorsChunk->chsclr.lStructSize    = sizeof(CHOOSECOLOR);  
  158.          lpColorsChunk->chsclr.hwndOwner      = ghWnd;
  159.          lpColorsChunk->chsclr.hInstance      = ghInst;
  160. //         lpColorsChunk->chsclr.rgbResult      = (DWORD)(lpColorsChunk->dwColor);
  161.          lpColorsChunk->chsclr.rgbResult      = (DWORD)NULL;
  162.          lpColorsChunk->chsclr.lpCustColors   = (LPDWORD)(lpColorsChunk->dwCustClrs);
  163.          lpColorsChunk->chsclr.Flags          = CC_SHOWHELP | CC_ENABLEHOOK | CC_FULLOPEN | CC_ENABLETEMPLATE;
  164.          lpColorsChunk->chsclr.lCustData      = 0L;
  165.           lpColorsChunk->chsclr.lpfnHook       = (FARHOOK)ColorHook;
  166.          lpColorsChunk->chsclr.lpTemplateName = (LPSTR)"ChooseColor";
  167.          break;
  168.  
  169.       default:
  170.  
  171.          break;
  172.  
  173.       }
  174.  
  175.    return;
  176. }
  177.  
  178.  
  179. //****************************************************************************
  180. // Function: AllocAndLockMem
  181. //
  182. // Purpose: To allocate and lock a chunk of memory for the CD structure
  183. //
  184. // Parameters:
  185. //    *hChunk == Pointer to handle of memory that will be allocated
  186. //    wSize   == Size of memory block to allocate
  187. //
  188. // Returns : LPSTR to block of memory allocated
  189. //
  190. // Comments:
  191. //
  192. // History:  Date       Author        Reason
  193. //           2/19/92    gregk         Created
  194. //****************************************************************************
  195. LPSTR FAR PASCAL AllocAndLockMem(HANDLE FAR *hChunk, WORD wSize)
  196. {
  197.    LPSTR lpChunk;
  198.  
  199.    *hChunk = GlobalAlloc(GMEM_FIXED, wSize);
  200.  
  201.    if (*hChunk)
  202.       {
  203.          lpChunk = GlobalLock(*hChunk);
  204.          if (!lpChunk)
  205.             {
  206.                GlobalFree(*hChunk);
  207.                ReportError(IDC_LOCKFAIL);
  208.                lpChunk=NULL;
  209.             }
  210.       }
  211.    else
  212.       {
  213.          ReportError(IDC_ALLOCFAIL);
  214.          lpChunk=NULL;
  215.       }
  216.    return(lpChunk);
  217. }
  218.  
  219. //****************************************************************************
  220. // Function: CPlApplet(HWND, WORD, LONG, LONG)
  221. //
  222. // Purpose:  The applets window procedure.  Called by Control Panel.     
  223. //
  224. // Parameters: None
  225. //         hCPlWnd == Handle to Control Panels window
  226. //             Msg == Current message 
  227. //         lParam1 == Varies depending on message 
  228. //         lParam2 == Varies depending on message 
  229. //
  230. // Returns : LONG, dependent on the message being processed.
  231. //
  232. // Comments: This function must be exported so CONTROL.EXE can do a 
  233. //           GetProcAddress() and ultimately send messages to this routine.
  234. //
  235. // History:  Date       Author        Reason
  236. //           3/23/92    gak           Created
  237. //****************************************************************************
  238. LONG FAR PASCAL CPlApplet(HWND hCPlWnd, WORD Msg, LONG lParam1, LONG lParam2)
  239. {
  240.  
  241. static char *szAppNames[]={"System Color Dropper"};
  242. static char *szDescriptions[]={"Set your system colors the cool way!"};
  243. static int icons[NUM_APPLETS] = { COOLCOLR_ICON };
  244.  
  245. int          i;
  246. WORD         wSize;
  247. LPNEWCPLINFO lpCPlInfo;
  248.  
  249.     switch (Msg) {
  250.  
  251.         case CPL_INIT:
  252.  
  253.             // first message to CPlApplet(), sent once only -
  254.             // returns a boolean for sucessful initialization
  255.  
  256.             ghWnd=hCPlWnd;
  257.  
  258. //Get the current color settings, so we can restore if the user hits Cancel.
  259.  
  260.             GetCurrentSysColors();
  261.  
  262. //Now, allocate memory for the CHOOSECOLOR structure
  263.  
  264.             wSize=sizeof(COLORSCHUNK);
  265.  
  266.             if (!(glpColorsChunk=(LPCOLORSCHUNK)AllocAndLockMem(&ghColorsChunk, wSize)))
  267.                return (LONG)FALSE;
  268.  
  269. //And initialize the
  270.             InitializeStruct(IDC_COLORS, (LPSTR)glpColorsChunk);
  271.  
  272.             return (LONG)TRUE;
  273.  
  274.         case CPL_GETCOUNT:
  275.  
  276.             // second message to CPlApplet(), sent once only -
  277.             // return the number of applets supported by this DLL.
  278.  
  279.             return NUM_APPLETS;
  280.             break;
  281.  
  282.         case CPL_NEWINQUIRE:
  283.  
  284.             // third message to CPlApplet() - it is sent as many times
  285.             // as the number of applets returned by CPL_GETCOUNT message
  286.  
  287.             lpCPlInfo = (LPNEWCPLINFO)lParam2;
  288.  
  289.             // lParam1 is an index ranging from 0 to (NUM_APPLETS - 1) 
  290.  
  291.             i = (int)lParam1;
  292.  
  293.             // your DLL must contain an icon and two string resources -
  294.             // hIcon is the icon handle, szName and szInfo are 
  295.             // strings describing the applets name and description
  296.  
  297.             lpCPlInfo->dwSize        = (DWORD)sizeof(NEWCPLINFO);
  298.             lpCPlInfo->dwFlags       = (DWORD)NULL;
  299.             lpCPlInfo->dwHelpContext = (DWORD)NULL;
  300.             lpCPlInfo->lData         = NULL;
  301.             lpCPlInfo->hIcon         = LoadIcon(ghInst, MAKEINTRESOURCE(icons[i]));
  302.             lstrcpy(lpCPlInfo->szName, (LPSTR)szAppNames[i]);
  303.             lstrcpy(lpCPlInfo->szInfo, (LPSTR)szDescriptions[i]);
  304.             *(lpCPlInfo->szHelpFile) = NULL;
  305.             
  306.             break;
  307.  
  308.         case CPL_SELECT:
  309.  
  310.             // one of your applets has been selected
  311.             // lParam1 is an index from 0 to (NUM_APPLETS - 1)
  312.             // lParam2 is the lData value associated with the applet
  313.  
  314.             break;
  315.  
  316.         case CPL_DBLCLK:
  317.  
  318.             // one of your applets has been double-clicked
  319.             // lParam1 is an index from 0 to (NUM_APPLETS - 1)
  320.             // lParam2 is the lData value associated with the applet
  321.  
  322.             LaunchApplet();
  323.             break;
  324.  
  325.         case CPL_STOP:
  326.  
  327.             // sent once for each applet prior to the CPL_EXIT msg
  328.             // lParam1 is an index from 0 to (NUM_APPLETS - 1)
  329.             // lParam2 is the lData value associated with the applet
  330.  
  331.             if (glpColorsChunk)  //Clean up memory for colors dialog
  332.                {
  333.                   GlobalUnlock (ghColorsChunk);
  334.                   GlobalFree   (ghColorsChunk);
  335.                }
  336.  
  337.             break;
  338.  
  339.         case CPL_EXIT:
  340.  
  341.             // last message, sent once only, before CONTROL.EXE calls
  342.             // FreeLibrary() on your DLL.
  343.  
  344.             break;
  345.  
  346.         default:
  347.             break;
  348.     }
  349.  
  350.     return 0L;
  351. }
  352.  
  353. //****************************************************************************
  354. // Function: LibMain(HANDLE, WORD, WORD, LPSTR)
  355. //
  356. // Purpose:  To initialize a global array that will be used if the user  
  357. //           CANCEL's his changes.
  358. //
  359. // Parameters: None
  360. //       hInstance == Handle to this instance
  361. //        wDataSeg == Our data segment
  362. //       wHeapSize == Heap size from the .DEF file
  363. //       lpCmdLine == The command line
  364. //
  365. // Returns : Nothing
  366. //
  367. // Comments:
  368. //
  369. // History:  Date       Author        Reason
  370. //           3/23/92    gak           Created
  371. //****************************************************************************
  372. BOOL FAR PASCAL LibMain(HANDLE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR lpCmdLine)
  373. {
  374.     ghInst = hInstance;
  375.     return TRUE;
  376. }
  377.  
  378.